﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using egypt.Logic.Data;
using egypt.Logic;
using System.Collections.Generic;

namespace egypt.Web.Admin
{
    public partial class countryManager : System.Web.UI.Page
    {
        DataAdapter da = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            da = new DataAdapter(System.Configuration.ConfigurationManager.ConnectionStrings["egypt"].ConnectionString);
            if (!this.Page.IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            List<country> tmpcountry = da.GetCountryList();
            this.GridView1.DataSource = tmpcountry;
            this.GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            this.GridView1.SelectedIndex = -1;
            //e.Cancel = true;
            if (e.NewSelectedIndex < this.GridView1.Rows.Count)
            {
                string countryId = ViewState[string.Format("Row_{0}", e.NewSelectedIndex)] == null ? "" : ViewState[string.Format("Row_{0}", e.NewSelectedIndex)].ToString();
                string country = this.GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;
                string flag = (this.GridView1.Rows[e.NewSelectedIndex].Cells[1].Controls[0] as Image).ImageUrl;
                bool IsShow = (this.GridView1.Rows[e.NewSelectedIndex].Cells[2].Controls[0] as CheckBox).Checked;
                this.hf_contryID.Value = countryId;
                this.txt_country.Text = country;
                this.img_flag.ImageUrl = flag;
                this.cb_isShow.Checked = IsShow;
                this.tb_Edit.Visible = true;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Bind();
            this.tb_Edit.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string countryId = this.hf_contryID.Value;
            string countryName = this.txt_country.Text;
            string flag = this.img_flag.ImageUrl;
            bool IsShow = this.cb_isShow.Checked;
            int cid = 0;
            if (int.TryParse(countryId, out cid))
            {
                country c = country.GetInstance(cid, countryName, flag, IsShow);
                da.UpdateCountry(c);
            }
            
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ViewState[string.Format("Row_{0}", e.Row.RowIndex)] = ((country)e.Row.DataItem).CountryID;

                LinkButton lb_del = e.Row.Cells[4].Controls[0] as LinkButton;
                if (lb_del != null)
                {
                    lb_del.CommandArgument = ((country)e.Row.DataItem).CountryID.ToString();
                    lb_del.OnClientClick = "return confirm('您确认要删除这条新闻？');";
                }
            }
        }

    }
}
